package com.aplikata.evok.ui;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import com.aplikata.bo.City;
import com.aplikata.bo.Node;
import com.aplikata.bo.User;
import com.aplikata.evok.bo.DomainPatient;
import com.aplikata.evok.bo.Patient;
import com.aplikata.evok.bo.PatientNode;
import com.aplikata.evok.constants.EvokConstants;
import com.aplikata.evok.service.EvokPatientService;
import com.aplikata.ui.UIBean;
import com.aplikata.util.FacesUtil;

/**
 * @ClassName: EvokPatientBean
 * @Description: TODO
 * @author Huyun
 * @date 17. 5. 2012. 09:10:03
 */
@ManagedBean(name = "evokPatientDialogBean")
@ViewScoped
public class EvokPatientDialogBean extends UIBean {
	private static final long serialVersionUID = 1L;
	private Long roleId;

	private Patient patient;
	private DomainPatient domainPatient;
	private City cityBirth;
	private City cityLive;
	private String username;
	private List<PatientNode> pnList;

	@ManagedProperty(value = "#{evokPatientService}")
	private EvokPatientService patientService;

	@PostConstruct
	public void init() {
		roleId = FacesUtil.getUserFromSession().getRole().getId();
		patient = new Patient();
		domainPatient = new DomainPatient();
	}

	public Patient getPatient() {
		patient = getDomainPatient().getPatient();
		if(patient == null){
			patient = new Patient();
		}
		return patient;
	}

	public void setPatient(Patient patient) {
		this.patient = patient;
	}

	public EvokPatientService getPatientService() {
		return patientService;
	}

	public void setPatientService(EvokPatientService patientService) {
		this.patientService = patientService;
	}

	public City getCityBirth() {
		return cityBirth;
	}

	public void setCityBirth(City cityBirth) {
		this.cityBirth = cityBirth;
	}

	public City getCityLive() {
		return cityLive;
	}

	public void setCityLive(City cityLive) {
		this.cityLive = cityLive;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public List<PatientNode> getPnList() {
		return pnList;
	}

	public void setPnList(List<PatientNode> pnList) {
		this.pnList = pnList;
	}

	public DomainPatient getDomainPatient() {
		if(domainPatient == null){
			domainPatient = new DomainPatient();
		}
		if (!FacesUtil.isKeyNull(domainPatient.getId()))
			return domainPatient;

		domainPatient = (DomainPatient) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("queryDomainPatient");

		if (domainPatient != null) {
			patient = domainPatient.getPatient();
		
			String sqlcity = "select {c1.*} from evk_patient p, _city c1 "
					+ " where c1.id = p.birthplace and p.id = "	+ patient.getId();
			List listcity = patientService.getSqlQuery(sqlcity).addEntity("c1", City.class).list();
			cityBirth = (City) listcity.get(0);

			sqlcity = "select {c1.*} from evk_domain_patient p, _city c1 "
					+ " where c1.id = p.city_id and p.id = " + domainPatient.getId();
			listcity = patientService.getSqlQuery(sqlcity).addEntity("c1", City.class).list();
			if(listcity == null || listcity.isEmpty()){
				cityLive = null;
			}else
				cityLive = (City) listcity.get(0);

			User user = (User) patientService.findSingleByNamedQuery(Patient.QY_FIND_USER,
					new Object[] { patient.getId() });
			username = user == null ? null : user.getUserName();

			String sql = "SELECT {epn.*},{n.*} FROM _access a, _node n, evk_patient_node epn where a.role_id = "
					+ roleId + " and a.node_id = n.id and n.type = " + EvokConstants.NODE_TYPE_PATIENT_BASIC
					+ " and epn.node_id = n.id and epn.domain_patient_id = " + domainPatient.getId()
					+ " order by n.sort;";

			List<?> list = patientService.getSqlQuery(sql).addEntity("epn", PatientNode.class).addEntity("n", Node.class)
					.list();
			if (list == null || list.isEmpty())
				return domainPatient;

			pnList = new ArrayList<PatientNode>();
			for (int i = 0; i < list.size(); i++) {
				Object[] tmp = (Object[]) list.get(i);
				PatientNode pn = (PatientNode) tmp[0];
				Node node = (Node) tmp[1];

				if (pn != null) {
					pn.setNode(node);
					pnList.add(pn);
				}
			}
		}else{
			domainPatient = new DomainPatient();
		}

		return domainPatient;
	}

	public void setDomainPatient(DomainPatient domainPatient) {
		this.domainPatient = domainPatient;
	}
}
